package com.accounting.client.frontend.common;

import com.accounting.client.remote.enumeration.Currency;

import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.form.SelectField;
import corelib.client.ui.widget.form.fomatters.ValueFormatter;

public class MoneyField {
	
	private CurrencyBox valueField;
	private SelectField<Currency> currencyField = new SelectField<Currency>(Currency.All);
	
	public MoneyField(Currency currency) {
		init(currency, WidgetListener.Nil);
	}
	
	public MoneyField(Currency currency, boolean valueEnabled) {
		init(currency, WidgetListener.Nil);
		valueField.setEnabled(valueEnabled);
	}
	
	public MoneyField(Currency currency, boolean valueEnabled, boolean currencyEnabled) {
		init(currency, WidgetListener.Nil);
		valueField.setEnabled(valueEnabled);
		currencyField.setEnabled(currencyEnabled);
	}
	
	public MoneyField(Currency currency, WidgetListener listener) {
		init(currency, listener);
	}
	
	private void init(final Currency currency, final WidgetListener listener) {
		valueField = new CurrencyBox(currency, listener);
		currencyField.setSelectedOption(currency);
		currencyField.setOptionFormatter(new ValueFormatter<Currency>(){
			@Override
			public String format(Currency value) {
				return value.getName();
			}
		});
	}
	
	public double getValue(){
		return valueField.getDouble(0.0);
	}
	
	public CurrencyBox getValueField(){
		return this.valueField;
	}
	
	public SelectField<Currency> getCurrencyField(){
		return this.currencyField;
	}
	
	public void setEnable(boolean enabled){
		this.valueField.setEnabled(enabled);
		this.currencyField.setEnabled(enabled);
	}
	
	public void setValueEnable(boolean enabled){
		this.valueField.setEnabled(enabled);
	}
	
	public void setCurrencyEnable(boolean enabled){
		this.currencyField.setEnabled(enabled);
	}
	
}
